package com.example.demo.task;

import com.example.demo.bean.Account;
import com.example.demo.bean.Order;
import com.example.demo.enums.OrderEnum;
import com.example.demo.service.AccountService;
import com.example.demo.service.OrderService;
import com.example.demo.service.impl.MQServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


@Transactional
public class Transfer {
    private Account merchant;
    private String orderId;
    private Double money;

    private boolean flag;

    public Transfer(Account account,String orderId,double money,boolean flag){
        this.merchant = account;
        this.orderId = orderId;
        this.money = money;
        this.flag = flag;
    }


    @Autowired
    private OrderService orderService;

    @Autowired
    private AccountService accountService;


    //只需要更新流量状态
    public void transfer(){
        //更新流水状态
        Order order = orderService.getById(orderId);
        order.setStatus(OrderEnum.LOCK.getKey());
        //更新余额
        if(flag){
            //最后一次更新余额
            merchant.setBalance(merchant.getBalance() + money);
            accountService.updateById(merchant);
        }
    }
}
